#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"plane.h"
void MainWindow::paintEvent(QPaintEvent *pe)
{
    QPainter* pa=new QPainter(this);
    QPixmap pm;
    //背景图片渲染
    Paint(":background/background.jpg",0,0,1400,1300,pa,pm);
    if(!start)
    {
        delete pa;
        return;
    }
    //结果场景渲染
    if(my_life<=0)
    {
        Paint(":background/gameover.png",0,0,1400,1300,pa,pm);
        delete pa;
        return;
    }
    //我方飞机渲染
    Paint(myplane,my_x,my_y,100,100,pa,pm);

    //飞机生成
    makeplane();

    //子弹生成
    makebullet();

    //更新我方子弹
    auto it=My_Bullets.begin();
    while(it!=My_Bullets.end())
    {
        bullet& bu=(*it);
        int x=bu.x;
        int y=bu.y;
        bool& exist=bu.exist;
        if(x<=0 || judge_hit(x,y,Planes))
        {
            exist=false;
        }
        if(exist)
        {
            Paint(my_bull,x,y,25,35,pa,pm);
            bu.RenewPos();
            ++it;
        }
        else {
            it=My_Bullets.erase(it);
        }
    }

    //更新敌方飞机
    QVector<plane> new_planes;
    for(int i=0;i<Planes.size();i++)
    {
        plane& p=Planes[i];
        int type=p.type;
        int x=p.x;
        int y=p.y;
        int life=p.life;
        if(!(life>0&&y<=1260))
        {
            if(life<=0)
            {
                my_score+=type*10;
                label_score->setText("得分: "+QString::number(my_score));
            }
            continue;
        }
        if(judege_crash(my_x,my_y,x,y)) //飞机发生碰撞
        {
            qDebug()<<"crash";
            my_life-=200;
            label_life->setText("生命值: "+QString::number(my_life));
            continue;
        }
        QString str;
        int size;
        if(type==1)
        {
            str=smallplane;
            size=75;
        }
        else if(type==2)
        {
            str=middleplane;
            size=100;
        }
        else if(type==3)
        {
            str=bigplane;
            size=200;
        }
        Paint(str,x,y,size,size,pa,pm);
        p.RenewPos();
        p.AddBullet();
        QVector<bullet>& bul=p.Bullets;
        QVector<bullet> new_bullets;
        for(int j=0;j<bul.size();j++)
        {
            bullet& bu=bul[j];
            int x=bu.x;
            int y=bu.y;
            int attack=bu.attack;
            if(abs(x-my_x)<=50&&abs(y-my_y)<=30)
            {
                my_life-=attack;
                label_life->setText("生命值: "+QString::number(my_life));
                qDebug()<<"attacked";
                continue;
            }
            Paint(bull,x,y,25,25,pa,pm);
            bu.RenewPos();
            new_bullets.push_back(bu);
        }
        p.Bullets=new_bullets;
        new_planes.push_back(p);
    }
    Planes=new_planes;
    delete pa;
}

void MainWindow::mouseMoveEvent(QMouseEvent *me)
{
    my_x=me->x()-50;
    my_y=me->y()-50;
}



void MainWindow::Paint(const QString &name,int x,int y,int w,int h,QPainter *pa, QPixmap &pm)
{
    pm.load(name);
    pa->drawPixmap(x,y,w,h,pm);
}

bool MainWindow::judge_gain_upgrade_source(int xr, int yr)
{
    QPair<int,int>pa={xr,yr};
    if(upgrade_source.find(pa)!=upgrade_source.end()&&upgrade_source[pa]==true)
    {
        return true;
    }
    return false;
}

bool MainWindow::judege_crash(int x1, int y1, int x2, int y2)
{
    if(abs(x1-x2)<=40&&abs(y1-y2)<=30)
    {
        return true;
    }
    return false;
}

void MainWindow::makeplane()
{
    if(plane_make_pre>=plane_make_fre)
    {
        plane_make_pre=0;
        int prob=rand()%1500;
        int x=rand()%1300+50;
        if(prob>=0&&prob<800)
        {
            plane p(100,200,1,x,0);
            Planes.push_back(p);
        }
        else if(prob>=800&&prob<1300)
        {
            plane p(100,400,2,x,0);
            Planes.push_back(p);
        }
        else
        {
            plane p(100,700,3,x,0);
            Planes.push_back(p);
        }
    }
    else
        plane_make_pre+=update_fre;
}

void MainWindow::makebullet()
{
    if(bullet_pre>=bullet_fre)
    {
        bullet_pre=0;
        bullet bul(attack,1,my_x+34,my_y-10);
        My_Bullets.push_back(bul);
    }
    else
        bullet_pre+=update_fre;
}

void MainWindow::judge_make_upgrade_source(int x, int y)
{
    int prob=rand()%1500;
    if(prob>=0&&prob<=100)
    {
        int xr=x/10;
        int yr=y/10;
        upgrade_source.insert({xr,yr},true);
    }
}

bool MainWindow::judge_hit(int x, int y, QVector<plane>& Planes)
{
    for(int i=0;i<Planes.size();i++)
    {
        plane& p=Planes[i];
        int X=p.x;
        int Y=p.y;
        if(abs(x-X)<=60&&abs(y-Y)<=30)
        {
            p.life-=attack;
            qDebug()<<"hit"<<p.type;
            return true;
        }
    }
    return false;
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(1400,1300);
    srand(time(nullptr));
    this->setMouseTracking(true);
    label_life=new QLabel(this);
    label_score=new QLabel(this);
    label_life->setGeometry(30,1080,300,200);
    label_life->setText("生命值: "+QString::number(my_life));
    QFont font("微软雅黑",22);
    label_life->setFont(font);
    label_life->setStyleSheet("color:red");

    label_score->setGeometry(30,1150,300,200);
    label_score->setText("得分: "+QString::number(my_score));
    label_score->setFont(font);
    label_score->setStyleSheet("color:red");
    timer_update=new QTimer(this);

    game_des=new QLabel(this);
    game_des->setText("按住鼠标左键控制飞机移动");
    game_des->setFont(font);
    game_des->setStyleSheet("color:red");
    game_des->setGeometry(450,200,800,600);

    game_start=new QPushButton(this);
    game_start->setText("开始游戏");
    game_start->setFont(font);
    game_start->setStyleSheet("color:blue;background:black");
    game_start->setGeometry(570,580,200,100);

    connect(game_start,&QPushButton::clicked,this,[&](){
        game_des->close();
        game_start->close();
        start=true;
        timer_update->start(update_fre);
    });

    connect(timer_update,&QTimer::timeout,this,[=](){
        update();
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

